package com.dy.user.controller;

import com.dy.user.service.IUserService;
import com.kaoshi.common.config.Result;
import com.dy.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-18
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        return userService.login(user);
    }

    @PostMapping("/logout/{token}")
    public Result getlogin(@PathVariable("token") String token) {
       return userService.logout(token);
    }

    @PostMapping("/loginRedis/{token}")
    public Result loginRedis(@PathVariable("token") String token) {
        return userService.loginRedis(token);
    }

    @PostMapping("/verification")
    public Result register(@RequestBody User user) throws Exception {
        userService.verification(user);
        return Result.ok("验证码已发送，请注意查收");
    }

    @PostMapping("/updateUser")
    public Result register(@RequestBody User user, HttpServletResponse response) {
        userService.updateUser(user, response);
        return Result.ok();
    }

    @PostMapping("/resetPassword")
    public Result resetpassword(@RequestBody User user){
        return userService.resetpassword(user);
    }

    @PostMapping("/passwordLogin")
    public Result passwordLogin(@RequestBody User user){
        return userService.passwordLogin(user);
    }

    @PostMapping("/sendSms")
    public Result sendSms(@RequestBody User user) {
        return userService.sendSms(user);
    }

    @PostMapping("/disableUser/{token}")
    public Result disableUser(@RequestBody User user, @PathVariable("token") String token) {
        return userService.disableUser(user, token);
    }

    @PostMapping("/updateDisable")
    public Result updateDisable(@RequestBody User user) {
        return userService.updateDisable(user);
    }

    @GetMapping("/getLoginUser/{satoken}")
    public User getLoginUser(@PathVariable("satoken") String satoken) {
        return userService.getLoginUser(satoken);
    }

    @PostMapping("uid/{id}")
    public User getUserById(@PathVariable("id") int id) {
        return userService.getUserById(id);
    }

    @PostMapping("/{token}")
    int getUserByToken(@PathVariable("token") String token){
        return userService.getUserByToken(token);
    };
    @GetMapping("/{id}")
    public User getDingUser2(@PathVariable("id") Integer id) {
        System.out.println("User:"+userService.selectUserById(id));
        return userService.selectUserById(id);
    }
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        userService.updateById(user);
        return Result.ok("修改成功");
    }
//    @Autowired
//    private OssUtil ossUtil;
//
//    @PostMapping("/upload")
//    public String upload(MultipartFile file) {
//        String filePath = ossUtil.uploadMultipartFile(file);
//        return filePath;
//    }
    @GetMapping("/outLogin/{id}")
    public Result outLogin(@PathVariable("id") Integer id) {
        return userService.outLogin(id);
    }
}
